package fivestar.ui.city;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import org.jdesktop.swingx.JXBusyLabel;
import org.jdesktop.swingx.JXTable;

public class City extends javax.swing.JDialog implements ActionListener, ItemListener,
		MouseListener {

	private static final long serialVersionUID = 1051024041739122174L;
	private JPanel pnlGuest, pnlNorth, pnlCenter;
	public JCheckBox chkCountry, chkCity, chkIdCity, chkDNI;
	private JXTable tblCustomer;
	public DefaultTableModel dtmTabla;
	private JScrollPane scpTable;
	private TableColumn tc[];
	String[] cabecera = { "DNI", "Full Name" };
	String[][] data = {};
	int fila;
	private JPanel pnlSouth;
	private JPanel panel;
	private JButton btnUpdate;
	private JButton btnNew;
	private JButton btnDelete;
	private JPanel panel_1;
	private JLabel lblNewLabel;
	private JTextField textField;
	private JPanel panel_2;
	private JLabel primero;
	private JLabel anterior;
	private JLabel numeroDePagina;
	private JLabel siguiente;
	private JLabel ultimo;

	final int elementosPagina = 25;
	int paginaSeleccionada = 1;
	private int elementos = 0;
	int paginas = 1;

	// CargadorResultados cargador = null;
	// BuscadorResultados buscador = null;
	boolean primeraCarga = true;
	private JLabel resultadosEncontrados;
	private JXBusyLabel animacionCarga;

	public City(Frame parent, boolean modal) {
		super(parent, modal);
		setResizable(false);
		initComponents();
		this.setLocationRelativeTo(null);
	}

	private void initComponents() {
		try {
			getContentPane().setLayout(new BorderLayout(0, 0));

			pnlGuest = new JPanel();
			pnlGuest.setBorder(BorderFactory.createTitledBorder(null,
					"", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Comic Sans MS", 0,
							12)));
			pnlGuest.setLayout(new BorderLayout());
			getContentPane().add(pnlGuest);

			pnlNorth = new JPanel();			
			pnlNorth.setLayout(new GridLayout(1, 6));
			pnlNorth.setBorder(BorderFactory.createTitledBorder(null,
					"Choose the fields to display",
					TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Comic Sans MS", 0,
							12)));
			pnlNorth.setBounds(0, 0, 728, 52);
			pnlGuest.add(pnlNorth, BorderLayout.NORTH);

			chkIdCity = new JCheckBox("Id City");
			chkIdCity.setHorizontalAlignment(SwingConstants.CENTER);
			chkIdCity.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
			pnlNorth.add(chkIdCity);
			chkIdCity.setSelected(true);
			chkIdCity.addItemListener(this);

			chkCity = new JCheckBox("City");
			chkCity.setHorizontalAlignment(SwingConstants.CENTER);
			chkCity.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
			chkCity.setSelected(true);
			chkCity.addItemListener(this);
			pnlNorth.add(chkCity);

			chkCountry = new JCheckBox("Country");
			chkCountry.setHorizontalAlignment(SwingConstants.CENTER);
			chkCountry.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
			chkCountry.setSelected(true);
			chkCountry.addItemListener(this);
			pnlNorth.add(chkCountry);

			pnlCenter = new JPanel();
			pnlCenter.setBorder(BorderFactory.createTitledBorder(null,
					"List Guest", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Comic Sans MS", 0,12)));
			pnlCenter.setBounds(0, 55, 728, 337);
			pnlCenter.setLayout(new BorderLayout(0, 0));
			pnlGuest.add(pnlCenter, BorderLayout.CENTER);

			animacionCarga = new JXBusyLabel();
			animacionCarga.setBounds(150, 150, 120, 50);
			animacionCarga.setHorizontalAlignment(SwingConstants.CENTER);
			//animacionCarga.setBackground(Color.BLACK);
			pnlCenter.add(animacionCarga, BorderLayout.CENTER);
			animacionCarga.setText("Loading...");
			animacionCarga.setBusy(true);

			// dtmTabla = new DefaultTableModel(data, cabecera);
			tblCustomer = new JXTable();
			tblCustomer.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
			tblCustomer.addMouseListener(this);
			scpTable = new JScrollPane(tblCustomer);
			pnlCenter.add(scpTable);

			pnlSouth = new JPanel();
			pnlGuest.add(pnlSouth, BorderLayout.SOUTH);
			pnlSouth.setLayout(new GridLayout(0, 3, 0, 0));

			panel_2 = new JPanel();
			pnlSouth.add(panel_2);

			lblNewLabel = new JLabel("City");
			panel_2.add(lblNewLabel);

			textField = new JTextField();
			panel_2.add(textField);
			textField.setColumns(10);

			resultadosEncontrados = new JLabel("0");
			panel_2.add(resultadosEncontrados);

			panel_1 = new JPanel();
			pnlSouth.add(panel_1);
			panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

			primero = new JLabel("|\u25C4");
			primero.addMouseListener(this);
			panel_1.add(primero);

			anterior = new JLabel("\u25C4");
			anterior.addMouseListener(this);
			panel_1.add(anterior);

			numeroDePagina = new JLabel("1");
			panel_1.add(numeroDePagina);

			siguiente = new JLabel("\u25BA");
			siguiente.addMouseListener(this);
			panel_1.add(siguiente);

			ultimo = new JLabel("\u25BA|");
			ultimo.addMouseListener(this);
			panel_1.add(ultimo);

			panel = new JPanel();
			pnlSouth.add(panel);

			btnNew = new JButton("New");
			btnNew.addActionListener(this);
			panel.add(btnNew);

			btnUpdate = new JButton("Update");
			panel.add(btnUpdate);

			btnDelete = new JButton("Delete");
			panel.add(btnDelete);

			// tc = new TableColumn[2];
			// for (int i = 0; i < 2; i++)
			// tc[i] = tblCustomer.getColumnModel().getColumn(i);

			setSize(633, 464);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
	}

	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					City frame = new City(null,true);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnNew){
			Register rg = new Register(null,true);
			rg.setVisible(true);
		}		
	}

}

